/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.engine.repository.typetransprocesser;

import com.inspur.edp.bef.bizentity.GspBizEntityElement;
import com.inspur.edp.cef.api.repository.ITypeTransProcesser;
import com.inspur.edp.cef.designtime.api.element.EnumIndexType;
import com.inspur.edp.cef.designtime.api.element.GspEnumValue;
import com.inspur.edp.cef.entity.condition.FilterCondition;
import com.inspur.edp.cef.repository.exception.CefRepositoryException;
import com.inspur.edp.cef.repository.utils.FilterUtil;
import java.sql.Connection;

public class EnumEngineVarcharProcesser implements ITypeTransProcesser {
    private GspBizEntityElement element;
    public EnumEngineVarcharProcesser(GspBizEntityElement element){
        this.element=element;
    }
    @Override public Object transType(FilterCondition condition, Connection connection) {
        return transType(FilterUtil.processStringValue(condition));
    }

    @Override public Object transType(Object o) {
        if(o==null){
            return null;
        }else if(o instanceof String){
            if("".equals(o))
                return "";
            return getIndex(element,(String) o);

        } else if(o instanceof Integer){
            return (Integer)o;
        }else {
            throw new CefRepositoryException("请输入正确的的枚举索引值或枚举编号值");
        }
    }

    private Object getIndex(GspBizEntityElement element, String value) {
        switch (element.getEnumIndexType()){
            case Integer:
                return getIntIndex(element,value);
            case String:
                return getStringIndex(element,value);
        }
        throw new CefRepositoryException(String.format(element.getLabelID() + "对应枚举索引类型不支持["+value+"]",element.getEnumIndexType().toString()));
    }
    private  Integer getIntIndex(GspBizEntityElement element,String value){
        for (GspEnumValue enumValue:element.getContainEnumValues()){

            //判断传进去的参数是枚举索引值
            String enumIndex=String.valueOf(enumValue.getIndex());
            if(value.equals(enumIndex)) {
                return enumValue.getIndex();
            }
            //判断传进来的参数是枚举编号
            String enumCode=enumValue.getValue();
            if(value.equals(enumCode)){
                return enumValue.getIndex();
            }
        }
        throw new CefRepositoryException(element.getLabelID()+ "对应的枚举项["+ value+ "]不正确,请输入正确的的枚举索引值或枚举编号值");
    }
    private String getStringIndex(GspBizEntityElement element,String value){
        for (GspEnumValue enumValue:element.getContainEnumValues()){

            //判断传进去的参数是枚举索引值
            String enumIndex=enumValue.getStringIndex();
            if(value.equals(enumIndex)) {
                return enumValue.getStringIndex();
            }
            //判断传进来的参数是枚举编号
            String enumCode = enumValue.getValue();
            if (value.equals(enumCode)) {
                return enumValue.getStringIndex();
            }
        }
        throw new CefRepositoryException(element.getLabelID()+ "对应的枚举项["+ value+ "]不正确,请输入正确的的枚举索引值或枚举编号值");
    }
    @Override public Object transType(Object o, boolean b) {
        return transType(o);
    }
}
